<?php
namespace app\common\controller;

use think\Controller;

/**
 * 基类公共控制器
 * @package app\admin\controller
 */
class Base extends Controller
{
    // 地址栏
    protected $query = [];

    // 系统变量
    protected $site = [];

    // 当前用户
    protected $user = [];
    
    // 默认消息
    protected $msg  = '参数错误';

    //过滤规则
    protected $filter = 'trim,htmlspecialchars';
    
    // 权限属性
    protected $auth = [
        'check'        => false,
        'rule'         => '',
        'cancel_login' => [],
        'cancel_limit' => [],
        'error_login'  => 'user/login/index',
        'error_limit'  => 'user/index/index',
        'redirect_url' => '',
    ];

    /**
     * 继承初始化方法
     */
    protected function initialize()
    {
        //过滤规则
        $this->request->filter($this->filter);
        //地址栏
        $this->query = $this->request->param();
        //用户信息
        $this->user  = model('common/User','loglic')->getCurrentUser();
        //系统变量
        $this->site['module']     = $this->request->module();
        $this->site['controll']   = strtolower($this->request->controller());
        $this->site['action']     = $this->request->action();
        $this->site['page']       = input('page/d', 1);
        $this->site['theme']      = 'default';
        $this->site['rootUrl']    = $this->request->rootUrl();
        $this->site['pathStatic'] = $this->site['rootUrl'].'/static';
        $this->site['pathUpload'] = url('index/upload/save',false,false);
        $this->site['jsConfig']   = [
            'require' => $this->site['module'].'/'.$this->site['controll'].'/'.$this->site['action'],
            'version' => config('app.app_version'),
        ];
    }
    
    /**
     * 权限验证方法 默认都需要登录 都需要鉴权 白名单除外
     * @author 老谭 <271513820@qq.com>
     * @return mixed
     */
    protected function authCheck()
    {
        //认证开关
        if(false == $this->auth['check']){
            return true;
        }
        //特殊权限（登录）
        if($this->auth['cancel_login'] == '*'){
            return true;
        }
        //权限规则
        if(!$this->auth['rule']){
            $this->auth['rule'] = $this->site['module'].'/'.$this->site['controll'].'/'.$this->site['action'];
        }
        //白名单（登录）既然不需要登录自然就不需要验证权限了
        if(in_array($this->auth['rule'], $this->auth['cancel_login'])){
            return true;
        }
        //白名单外需要验证登录与权限
        if($this->user['user_id'] < 1){
            //回跳URL
            if($this->auth['redirect_url']){
                session('redirect_url',$this->auth['redirect_url']);
            }
            //登录跳转
            $this->error('请先登录', $this->auth['error_login']);
        }
        //特殊权限（鉴权）
        if($this->auth['cancel_limit'] == '*'){
            return true;
        }
        //白名单（鉴权）
        if(in_array($this->auth['rule'], $this->auth['cancel_limit'])){
            return true;
        }
        //不需要鉴权的白名单里没有此规则需验证是否有对应权限关系
        if ( false == model('common/Auth','loglic')->check($this->auth['rule'], $this->user['user_roles'], $this->user['user_caps']) ) {
            $this->error('您没有权限', $this->auth['error_limit']);
        }
    }
}